<?php
/**
 * Created by PhpStorm.
 * User: liuguang
 * Date: 2017/12/22
 * Time: 下午9:13
 */
require_once "./Trans.php";
//支持任意多关键字的数组排序
class Order
{
    use Trans;
    /**
     * Order constructor.
     * @param $array 要排序的数组
     * @param $key   array 按照数组中的那些键排序(排序的键的值可以是数字，字母，汉字，不能是特殊字符 如果为数字0则排在最末尾)
     * @param $noun  array 每个键的权重（权重越大，排序越靠前）
     * @param $maxMain   array 排序键的最大值和最小值 默认 null
     * @param string $orderBy 是升序还是降序 'DESC' 降序 ASC 升序
     */
    public function orders($array,$key,$noun,$maxMain=null,$orderBy = 'DESC')
    {
        //获取排序的数据的最大值和最小值
        if (is_null($maxMin)){
            $maxMin = $this->findMaMin($array,$key,$noun);
        }

        //计算各排序键的权重比
        $nounRes = $this->caculate($maxMin);
        //数组排序
        $array   = $this->orderaByDo($array,$nounRes,$key,$orderBy);
        return $array;
    }

    public function orderaByDo($array,$nounRes,$key,$orderBy)
    {
        $num = 0;
        foreach ($array as $m => $n){
            foreach ($n as $a => $b){
                if (in_array($a,$key)){
                    $num = $num + $b * $nounRes[$a] + 1;
                }
            }
            $array[$m]['orderValue'] = $num;
            $num = 0;
        }
        //var_dump($array);
        //排序
        $len = count($array);
        for($i=1;$i<$len;$i++)
        { //该层循环用来控制每轮 冒出一个数 需要比较的次数
            for($k=0;$k<$len-$i;$k++)
            {
                if ($orderBy == 'DESC'){
                    if($array[$k]['orderValue']<$array[$k+1]['orderValue'])
                    {
                        $tmp=$array[$k+1];
                        $array[$k+1]=$array[$k];
                        $array[$k]=$tmp;
                    }
                }else{
                    if($array[$k]['orderValue']>$array[$k+1]['orderValue'])
                    {
                        $tmp=$array[$k+1];
                        $array[$k+1]=$array[$k];
                        $array[$k]=$tmp;
                    }
                }

            }
        }
        return $array;
    }
    //按照权重计算乘积值
    public function caculate($maxMin)
    {
        $numValue = array_values($maxMin);
        $keysValue= array_keys($maxMin);
        $plural = [];
        $num    = 1;
        foreach ($numValue as $m => $n){
            if (!isset($numValue[$m+1]) && $m != 1) break;
                $plural[$keysValue[$m]] = 1;
                if ($m['max'] * $num <= ($m + 1)['min']){
                    $plural[$keysValue[$m + 1]] = 1;
                }else{
                    $plural[$keysValue[$m + 1]] = ceil(($m['max'] * $num) / ($m + 1)['min']);
                    $num = $plural[$keysValue[$m + 1]];
                }
        }
        return $plural;

    }
    /**
     * 获取数据中要排序键的最大值和最小值
     * @param $array
     * @param $key
     */
    public function findMaMin($arrays,$keys,$noun)
    {
        $array = [];
        $keyArray = array_combine($keys,$noun);
        sort($kwyArray);
        foreach ($arrays as $key=>$value){
            foreach ($value as $m=>$n){
                    foreach ($keyArray as $z => $y)
                    {
                        if ($m == $z){
                            $n = $this->trans($n);
                            $array[$z][] = $n;
                        }
                    }
            }
        }
        var_dump($array);
        die;
        $maxMin = [];
        foreach ($array as $a => $b){
            $max = max($array[$a]);
            $min = min($array[$a]);
            if (0 == $max || 0 == $min){
                $max = $min = 1;
            }

            $maxMin[$a]['max'] = $max;
            $maxMin[$a]['min'] = $min;
        }
        var_dump($maxMin);
        die;
        return $maxMin;
    }

}